package com.self.admin.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.self.admin.model.SysUser;
import com.self.admin.service.SysUserService;
import com.self.common.exception.BusinessException;
import com.self.common.exception.RestError;
import com.self.common.response.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.List;

/**
 * @version v1.0
 * @ClassName: SysUserController
 * @Description:
 * @Author: Mac
 * @Date: 2022/6/4 18:27
 */

@Api(tags = "员工管理")
@RestController
@RequestMapping("/users")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService ;

    @GetMapping
    @ApiOperation(value = "员工分页查询")
    @PreAuthorize("hasAuthority('sys_user_query')")
    public R<Page<SysUser>> getPage(@ApiIgnore Page<SysUser> page , String mobile , String fullname){
        page.addOrder(OrderItem.desc("last_update_time"));
        Page<SysUser> pageData =  sysUserService.getPage(page,mobile ,fullname);
        return R.ok(pageData);
    }

    @PostMapping
    @ApiOperation(value = "新增员工")
    @PreAuthorize("hasAuthority('sys_user_create')")
    public R addUser(@Validated @RequestBody SysUser sysUser){
        sysUserService.addUser(sysUser);
        return R.ok();
    }

    @PatchMapping
    @ApiOperation(value = "更新员工")
    @PreAuthorize("hasAuthority('sys_user_update')")
    public R updateUser(@Validated @RequestBody SysUser sysUser){
        if (sysUser.getId() == null){
            throw new BusinessException("员工id不能为空");
        }
        sysUserService.updateUser(sysUser);
        return R.ok();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除用户")
    @PreAuthorize("hasAuthority('sys_user_delete')")
    public R deleteUser(@RequestBody List<Long> ids){
        sysUserService.deleteUser(ids);
        return R.ok();
    }
}
